package com.tanhua.dubbo.api;

import com.tanhua.domain.db.PageResult;
import com.tanhua.domain.mg.FollowUser;
import com.tanhua.domain.mg.Friend;
import com.tanhua.domain.mg.Publish;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class FriendApiImpl implements FriendApi {

    //注入Mongotemplate对象
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 建立好友关系
     */
    @Override
    public void save(Long userId, Long friendId) {
        //创建一个用于存储好友表的对象
        Friend friend = new Friend();
        //补全好友的信息
        friend.setId(ObjectId.get());//主键
        friend.setCreated(System.currentTimeMillis());//创建时间
        friend.setUserId(userId);//用户id
        friend.setFriendId(friendId);//好友id
        //调用
        mongoTemplate.save(friend, "tanhua_users");
    }

    /**
     * 删除好友关系
     */
    @Override
    public void remove(Long currentUserId, Long focusId) {
        //设置查询条件
        Query query = new Query();
        query.addCriteria(Criteria
                .where("userId").is(currentUserId)
                .and("friendId").is(focusId));
        //调用删除方法
        mongoTemplate.remove(query, Friend.class);

    }

    /**
     * 查询好友关系
     *
     * @param userId
     * @param friendId
     * @return
     */
    @Override
    public Friend queryFriend(Long userId, Long friendId) {
        //创建一个条件
        Query query = new Query();
        query.addCriteria(Criteria
                .where("userId")
                .is(userId).and("friendId")
                .is(friendId));
        //查询好友表
        return mongoTemplate.findOne(query, Friend.class);
    }

    /**
     * 查看好友列表
     */
    @Override
    public PageResult<Friend> findFriendsById(Integer page, Integer pagesize, Long currentUserId, String keyword) {

        //创建分页查询条件 根据当前用户id查询好友信息
        Query query = new Query(Criteria.where("userId").is(currentUserId));
        query.limit(pagesize).skip((page - 1) * pagesize);
        //查询条件
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        //计算总记录数
        long count = mongoTemplate.count(query, Friend.class);
        //计算总页数
        long pages = (count / pagesize) + (count % pagesize > 0 ? 1 : 0);
        //8.封装PageResult
        PageResult<Friend> publishPageResult = new PageResult<>();
        publishPageResult.setItems(friends);
        publishPageResult.setPage(page);
        publishPageResult.setPagesize(pagesize);
        publishPageResult.setCounts((int)count);
        publishPageResult.setPages((int)pages);
        return publishPageResult;
    }
    /**
     * 根据当前ID查询好友数量
     * @param currentUserId
     * @return
     */
    @Override
    public Integer countFriend(Long currentUserId) {
        //设置查询条件
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(currentUserId));
        //查询
        long count = mongoTemplate.count(query, Friend.class);
        return (int)count;
    }
}
